//
//  DYSearchView.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2020/12/31.
//  Copyright © 2020 叁拾叁. All rights reserved.
//

import UIKit
import  RxSwift

class DYSearchView: SYBaseView {
    
    public var clickChoiceClosure: ((Int) -> Void)?
    
    
    fileprivate var bgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = UIColor(hex: 0xF2F2F2)
        v.layer.cornerRadius = 5
        return v
    }()
    
    public lazy var search: VGImageButton = {
        
        let v: VGImageButton = VGImageButton()
        v.imageView.image = UIImage(named: "zixun_search")
        return v
    }()
    
    public lazy var close: VGImageButton = {
        
        let v: VGImageButton = VGImageButton()
        v.imageView.image = UIImage(named: "zixun_search_close")
        v.btn.addTarget(self, action: #selector(closeBtAction), for: .touchUpInside)
        return v
    }()
    
    
    let tf: UITextField = {
        let v: UITextField = UITextField()
        v.font = 14.autoFontSize()
        v.placeholder = "搜索问题..."
        return v
    }()
    
    override func initViews() {
        
        self.backgroundColor = .white
        //当文本框内容改变时，将内容输出到控制台上
        tf.rx.text.orEmpty.asObservable()
            .subscribe(onNext: {
                print("您输入的是：\($0)")
            })
            .disposed(by: disposeBag)
        //按下 return 键
//        tf.rx.controlEvent(.editingDidEndOnExit).subscribe(onNext: {
//            [weak self] (_) in
//            if let `self` = self {
//                if let c = self.clickChoiceClosure {
//                    c(1)
//                }
//            }
//        }).disposed(by: disposeBag)
        // 编辑完成
        tf.rx.controlEvent(.editingDidEnd).subscribe(onNext: {
            [weak self] (_) in
            if let `self` = self {
                if let c = self.clickChoiceClosure {
                    c(1)
                }
            }
        }).disposed(by: disposeBag)
        
        addSubview(bgView)
        bgView.addSubview(search)
        bgView.addSubview(tf)
        bgView.addSubview(close)
        bgView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.height.equalTo(30.0.autoWidth())
            make.left.equalToSuperview().offset(15.0.autoWidth())
            make.right.equalToSuperview().offset(-15.0.autoWidth())
        }
        search.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.width.height.equalTo(12.0.autoWidth())
            make.left.equalToSuperview().offset(10.0.autoWidth())
        }
        tf.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(search.snp.right).offset(6.0.autoWidth())
            make.right.equalTo(close.snp.left).offset(-6.0.autoWidth())
            make.height.equalToSuperview()
        }
        close.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.width.height.equalTo(11.0.autoWidth())
            make.right.equalToSuperview().offset(-14.0.autoWidth())
        }
    }
    
    @objc func closeBtAction(button: UIButton) {
        if let c = clickChoiceClosure {
            c(0)
            self.tf.text = ""
        }
    }
}
